package org.example;

import org.example.enums.EndpointEnum;
import org.example.plugin.HeartBeat;
import org.example.processor.MyProcessor;
import org.example.protocol.StringProtocol;
import org.smartboot.socket.extension.plugins.StreamMonitorPlugin;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;

import java.io.IOException;
import java.util.Scanner;

/**
 * @author Tamako
 * @since 2025-07-18 14:53
 */
public class Client {
    public static void main(String[] args) throws IOException, InterruptedException {
        MyProcessor myProcessor = new MyProcessor(EndpointEnum.CLIENT);
        myProcessor.addPlugin(new HeartBeat());
        // myProcessor.addPlugin(new StreamMonitorPlugin<>());
        AioQuickClient aioQuickClient = new AioQuickClient("127.0.0.1", 8888, new StringProtocol(), myProcessor);
        AioSession session = aioQuickClient.start();

        WriteBuffer writeBuffer = session.writeBuffer();
        boolean flag = true;
        while (flag) {
            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();
            if ("exit".equals(next)) {
                flag = false;
            }
            writeBuffer.writeInt(next.getBytes().length);
            writeBuffer.write(next.getBytes());
            writeBuffer.flush();
        }

        // int i = 0;
        // while (true) {
        //     i++;
        //     WriteBuffer writeBuffer = session.writeBuffer();
        //     if (i % 5 == 0) {
        //         byte[] content = "heart message".getBytes();
        //         writeBuffer.writeInt(content.length);
        //         writeBuffer.write(content);
        //         Thread.sleep(2000L);
        //     } else {
        //         byte[] content = ("content message -" + i).getBytes();
        //         writeBuffer.writeInt(content.length);
        //         writeBuffer.write(content);
        //         Thread.sleep(1000L);
        //     }
        // }
    }
}
